package com.game.grid;

import java.util.LinkedList;
import javax.microedition.khronos.opengles.GL10;

import com.mcodesal.framework.ResourceProvider;
import com.mcodesal.framework.Sprite2D;
import com.mcodesal.framework.ResourceProvider.Texture;
import com.mcodesal.framework.utils.Vector2D;

public class Player {
	
	private Vector2D myLocation;
	public int size;
	public float headRotation;
	public Sprite2D square;
	public Sprite2D head;
	public Sprite2D body;
	public Sprite2D tail;
	public LinkedList<Vector2D> positions;
	public long lastTime = -1;
	public boolean add180 = false;
	
	public Player(int x, int y, int boxSize) {  //x,y cords put in are the top left corner.
		myLocation = new Vector2D(x, y);
		headRotation = 0;
		positions = new LinkedList<Vector2D>();
		positions.addFirst(new Vector2D(myLocation.x, myLocation.y));
		size = boxSize;
		
		square = new Sprite2D(16,16);
		head = new Sprite2D(32,32);
		body = new Sprite2D(32,32);
		tail = new Sprite2D(32,32);
		//Mid-Size
		/*
		square = new Sprite2D(16, 16);
		head = new Sprite2D(16,16);
		body = new Sprite2D(16,16);
		tail = new Sprite2D(16,16);
		*/
		square.setTexture(ResourceProvider.getInstance().getTexture(Texture.PLAYER_DOT));
		head.setTexture(ResourceProvider.getInstance().getTexture(Texture.PLAYER_HEAD));
		body.setTexture(ResourceProvider.getInstance().getTexture(Texture.PLAYER_BODY));
		tail.setTexture(ResourceProvider.getInstance().getTexture(Texture.PLAYER_TAIL));
	}
	
	public void moveUp(int speed) { //function takes in the speed at which you want the boxes to move down screen
		myLocation.y += speed*2;
	}
	
	public int getLeftSide() {
		return myLocation.intX();
	}
	public int getRightSide() {
		return myLocation.intX() + size;
	}
	public int getTopSide() {
		return myLocation.intY();
	}
	public int getBottomSide() {
		return myLocation.intY() + size;
	}
	
	public void setLoc(int x, int y) {
		myLocation.x = x;
		myLocation.y = y;
	}
	
	public int getBoxSize() {
		return size;
	}
	
	public void addToX(int var) {
		myLocation.x += var;
	}
	public void update(long currTime) {
		if(lastTime == -1) {
			lastTime = currTime;
		}
		if(currTime-lastTime > 100) {
			if(positions.size() == 3) {
				positions.removeLast();
			}
			positions.addFirst(new Vector2D(myLocation.x, myLocation.y));
			
			Vector2D glowWormSlope = positions.getLast().subtract(positions.getFirst());
			glowWormSlope.normalize();
			if(glowWormSlope.intY() != 0 && glowWormSlope.intX() != 0) {
				headRotation = (float) ( Math.atan(glowWormSlope.intY()/glowWormSlope.intX())*180/Math.PI);
				glowWormSlope.scale(.5);
				head.x = myLocation.intX() + glowWormSlope.intX();
				head.y = myLocation.intY() + glowWormSlope.intY();
				body.x = (int) (myLocation.intX() + glowWormSlope.intX()*1.5);
				body.y = (int) (myLocation.intY() + glowWormSlope.intY()*1.5);
				tail.x = (int) (myLocation.intX() + glowWormSlope.intX()*2);
				tail.y = (int) (myLocation.intY() + glowWormSlope.intY()*2);
			}
			else {
				headRotation = 90;
				head.x = myLocation.intX();
				head.y = myLocation.intY() + 16;
				body.x = (int) (myLocation.intX());
				body.y = (int) (myLocation.intY() + 32);
				tail.x = (int) (myLocation.intX());
				tail.y = (int) (myLocation.intY() + 48);
			}
			if(glowWormSlope.intY() > 0 && glowWormSlope.intX() < 0) {
				add180 = true;
			}
			else if(glowWormSlope.intY() < 0 && glowWormSlope.intX() < 0) {
				add180 = true;
			}
			else { 
				add180 = false;
			}
			
			lastTime = currTime;
		}
	}
	public void draw(GL10 gl) {
		/*
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
	    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
	    */   
		gl.glPushMatrix();
        gl.glTranslatef(myLocation.intX() - 16/2, myLocation.intY(), 0);
        square.draw(gl);
        gl.glPopMatrix();
        
        gl.glPushMatrix();
        gl.glTranslatef(tail.x - 32/2, tail.y, 0);
        tail.draw(gl);
        gl.glPopMatrix();
        
        gl.glPushMatrix();
        gl.glTranslatef(body.x - 32/2, body.y, 0);
        body.draw(gl);
        gl.glPopMatrix();
        
        gl.glPushMatrix();
        gl.glTranslatef(head.x - 32/2, head.y, 0);
        gl.glTranslatef(16f, 16f, 0f);
        if(add180)
        	gl.glRotatef(90+headRotation, 0, 0, 1f);
        else
        	gl.glRotatef(headRotation-90, 0, 0, 1f);
        gl.glTranslatef(-16f,-16f, 0f);
        head.draw(gl);
        gl.glPopMatrix();
        /*
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		*/
	}
}